/**
 * @(#) DailyCostProxy.as
 *
 * @date		2009. 04. 6.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 일비계산 Proxy - Proxy
 *
 */
package com.skt.common_hr.biztrip_sac.model
{
	import com.skt.common_hr.biztrip_sac.BizTripFacade;
	import com.skt.common_hr.biztrip_sac.model.vo.BizTripPlaceVO;
	import com.skt.common_hr.biztrip_sac.model.vo.DailyCostVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class DailyCostProxy extends CommonProxy
	{
		public static const NAME:String = "DailyCostProxy";

		public static const LOAD_DAILY_COST_MULTI_COMPLETE:String = "loadDailyCostMultipleComplete";
		public static const LOAD_DAILY_COST_MULTI_FAILED:String = "loadDailyCostMultipleFailed";

		public var costList:ArrayCollection;
		public var errCode:int;
		public var errText:String;
		public var pernr:String;

		private var days:int;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function DailyCostProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번,대상월
		 * @return void
		 */
		public function load(pernr:String, type:String, days:Number, begda:Date, endda:Date,
								acPlace:ArrayCollection=null,
								vehicle:Boolean=false, seminar:Boolean=false, gubun:String="", sStatusView:String=''):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 	= pernr; 
			param["I_TRTYPE"] 	= type;
			
			
			param["I_TRDAYS"] 	= days.toString();
			param["I_OIL"] 		= vehicle ? "X" : "";
			param["I_BEGDA"] 	= DateUtil.toBasicString(begda);
			param["I_ENDDA"] 	= DateUtil.toBasicString(endda);
			
			//0516 Z_HR_T009에만 있는 param 주석
			//param["I_LAGBN"] 	= gubun;		
			//param["I_EGBN"]		= seminar ? "X" : "";
			
			this.days = days;

			var tables:Object = new Object();
			// I_TAB
 			var tArr:Array = new Array();
 			var bHavekDrikm:Boolean = false;
	 		for each(var rec:BizTripPlaceVO in acPlace)
	 		{
	 			var data:Object = new Object;
	 			data.BEGDA = DateUtil.toBasicString(rec.BEGDA);
	 			data.ENDDA = DateUtil.toBasicString(rec.ENDDA);
	 			
	 			data.BREN = rec.BREN;
	 			data.BREN1 = rec.BREN1;
	 			data.TGBN = rec.TGBN;
	 			data.DRIKM = rec.DRIKM
				if(rec.TGBN=='2' || rec.TGBN=='3'){
					data.I_OIL = "X";	
				}
				if(rec.DRIKM > 0) {
					bHavekDrikm = true;
				}
	 			tArr.push(data);
	 		}
	 		
// 일비 계산이 기존 조회함수와 중복됨으로. 거리(전부 0)인지와 현재 모드를 체크해서 (품의변경) ..
// itab을 초기화 시키면  sap에서 Z_HR_T009을 호출하기로함.	 		
//	 		if(sStatusView != 'RE'  && bHavekDrikm==true) {	
//	 		} else {
//	 			tArr = new Array();
//	 		}	 		 
	 		tables["ITAB"] = tArr;	 		
			saveData("Z_HR_T019", param, tables, result, fault);


			//loadData("Z_HR_T009", param, result, fault);
			//0512 일비 계산변경
			//loadData("Z_HR_T019", param, result, fault);
		}



		public function loadDayPay(type:String, BEGDA:Date, acPlace:ArrayCollection):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 	= Variables.myPernr;
			param["I_BEGDA"] 	= DateUtil.toBasicString(BEGDA);
			param["I_TRTYPE"] 	= type;
			
			var tables:Object = new Object();
 			var tArr:Array = new Array();
	 		for each(var rec:BizTripPlaceVO in acPlace)
	 		{
	 			var data:Object = new Object;
	 			data.BEGDA = DateUtil.toBasicString(rec.BEGDA);
	 			data.ENDDA = DateUtil.toBasicString(rec.ENDDA);
	 			
	 			data.BREN = rec.BREN;
	 			data.BREN1 = rec.BREN1;
	 			data.TGBN = rec.TGBN;
	 			data.DRIKM = rec.DRIKM
	 			
				if(rec.TGBN=='2' || rec.TGBN=='3'){
					//data.I_OIL = "X";	
				}
				 
	 			tArr.push(data);
	 		}
	 		tables["ITAB"] = tArr;
			saveData("Z_HR_T019", param, tables, resultMulti, faultMulti);
		}
		
		public function loadMulti(type:String, BEGDA:Date, tripList:Array, acPlace:ArrayCollection=null):void
		{
			var param:Object = new Object();
			param["I_BEGDA"] 	= DateUtil.toBasicString(BEGDA);
			param["L_TRTYPE"] 	= type;
			var tables:Object = new Object();
			tables["ITAB"] = tripList;

 			var tArr:Array = new Array();
	 		for each(var rec:BizTripPlaceVO in acPlace)
	 		{
	 			var data:Object = new Object;
	 			data.BEGDA = DateUtil.toBasicString(rec.BEGDA);
	 			data.ENDDA = DateUtil.toBasicString(rec.ENDDA);
	 			
	 			data.BREN = rec.BREN;
	 			data.BREN1 = rec.BREN1;
	 			data.TGBN = rec.TGBN;
	 			data.DRIKM = rec.DRIKM
	 			
				if(rec.TGBN=='2' || rec.TGBN=='3'){
					//data.I_OIL = "X";	
				}
				 
	 			tArr.push(data);
	 		}
	 		tables["ITAB1"] = tArr;
			//saveData("Z_HR_T011", param, tables, resultMulti, faultMulti);
			//0516
			saveData("Z_HR_T021", param, tables, resultMulti, faultMulti);
		}

		public function loadForeign(BEGDA:Date, tripList:Array):void
		{
			var param:Object = new Object();
			param["I_BEGDA"] 	= DateUtil.toBasicString(BEGDA);
			param["L_TRTYPE"]	= "02";
			var tables:Object = new Object();
			tables["ITAB"] = tripList;

			//Alert.show("loadForeign2 : " + param["I_BEGDA"] + ", " + tripList[0], "확인");
			saveData("Z_HR_T011", param, tables, resultForeign, faultForeign);
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			errCode = event.result["RETCODE"];
			errText = event.result["RETTEXT"];
			if(event.result["RETCODE"]==0) {
				costList = parseDailyCost(event.result.OTAB);
				facade.sendNotification(BizTripFacade.LOAD_DAILY_COST_COMPLETE, this);
			}
			else {
				trace( "failed to 출장 리스트: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(BizTripFacade.LOAD_DAILY_COST_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			errCode = -1;
			errText = "호출실패";
			trace("Fault message in 출장리스트: " + event.fault.faultString);
			facade.sendNotification(BizTripFacade.LOAD_DAILY_COST_FAILED, this);
			CursorManager.removeBusyCursor();
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function resultMulti(event:ResultEvent):void
		{
			errCode = event.result["RETCODE"];
			errText = event.result["RETTEXT"];
			trace("resultMulti: " + errCode);

			if(event.result["RETCODE"]==0) {
				costList = parseDailyCostMulti(event.result.OTAB);
				facade.sendNotification(LOAD_DAILY_COST_MULTI_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_DAILY_COST_MULTI_FAILED, this);
			}
		}


		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function faultMulti(event:FaultEvent):void
		{
			errCode = -1;
			errText = "호출실패";
			facade.sendNotification(LOAD_DAILY_COST_MULTI_FAILED, this);
		}

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function resultForeign(event:ResultEvent):void
		{
			errCode = event.result["RETCODE"];
			errText = event.result["RETTEXT"];
			trace("resultforeign: " + errCode);
			if(event.result["RETCODE"]==0) {
				costList = parseDailyCostForeign(event.result.OTAB);
				facade.sendNotification(BizTripFacade.LOAD_DAILY_COST_FOREIGN_COMPLETE, this);
			}
			else {
				trace( "failed to 출장 리스트: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(BizTripFacade.LOAD_DAILY_COST_FOREIGN_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function faultForeign(event:FaultEvent):void
		{
			errCode = -1;
			errText = "호출실패";
			trace("Fault message in 출장리스트: " + event.fault.faultString);
			facade.sendNotification(BizTripFacade.LOAD_DAILY_COST_FOREIGN_FAILED, this);
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseDailyCost(srcArr:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			var vo:DailyCostVO;

			for each(var rec:Object in srcArr)
			{
				vo = new DailyCostVO();
				vo.COSTGBN	= rec["COSTGBN"];
				vo.WAERS	= rec["WAERS"];
				vo.AMT		= rec["AMT"];
				if(vo.WAERS=="KRW") {
					vo.AMT = Math.round(vo.AMT*100);
				}
				vo.days		= this.days;
				list.addItem(vo);
			}
			if(list.length == 0) {
				vo = new DailyCostVO();
				vo.COSTGBN	= "";
				vo.WAERS	= "";
				vo.AMT		= 0;
				list.addItem(vo);
			}
			return list;
		}

		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseDailyCostMulti(srcArr:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in srcArr)
			{
				var vo:DailyCostVO = new DailyCostVO();
				vo.COSTGBN	= rec["COSTGBN"];
				vo.PERNR	= StringUtil.trimFront(rec["PERNR"], "0");;
				vo.WAERS	= rec["WAERS"];
				vo.AMT		= rec["AMT"];
				vo.AMT1		= rec["AMT1"];
				if(vo.WAERS=="KRW") {
					vo.AMT = Math.round(vo.AMT*100);
					vo.AMT1 = Math.round(vo.AMT1*100);
				}
				vo.days		= this.days;
				list.addItem(vo);
			}
			return list;
		}


		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseDailyCostForeign(srcArr:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in srcArr)
			{
				var vo:DailyCostVO = new DailyCostVO();
				vo.COSTGBN	= rec["COSTGBN"];
				vo.PERNR	= StringUtil.trimFront(rec["PERNR"], "0");;
				vo.WAERS	= rec["WAERS"];
				vo.AMT		= rec["AMT"];
				if(vo.WAERS=="KRW") {
					vo.AMT = Math.round(vo.AMT*100);
				}
				vo.days		= this.days;
				list.addItem(vo);
			}
			if(list.length == 0) {
				vo = new DailyCostVO();
				vo.COSTGBN	= "";
				vo.WAERS	= "";
				vo.AMT		= 0;
				list.addItem(vo);
			}
			return list;
		}

		public function getDailyCost(pernr:String):DailyCostVO
		{
			for each(var vo:DailyCostVO in costList) {
				if(vo.PERNR == pernr) return vo;
			}
			return null;
		}
	}
}